package top.cardone.validator.func;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.validator.GenericValidator;

import java.util.Map;
import java.util.Objects;

/**
 * Created by Administrator on 2016/9/19.
 */
@Log4j2
public class MaxValidatorFunc extends RequiredValidatorFunc {
	public MaxValidatorFunc() {
		this.code = "validator.max";
	}

	@Override
	public String func(Map<String, Object> map, String key, Object rule, Object message) {
		log.debug(this.code + ":" + key);

		Double val = MapUtils.getDouble(map, key, new Double(0));

		if (Objects.isNull(val)) {
			return null;
		}

		Double max = (Double) rule;

		if (GenericValidator.maxValue(val, max)) {
			return null;
		}

		return this.getMessage(message, new Object[]{max});
	}
}